package com.haixue.android.academy.utils;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;

import java.util.Arrays;
import java.util.concurrent.ConcurrentHashMap;

import cn.woblog.android.common.utils.SharePreferenceUtils;
import cn.woblog.android.http.RequestParams;

/**
 * Created by ren on 2015/5/22 0022.
 */
public class MyParams extends  BaseLog{
    private final Context context;
    private final SharePreferenceUtils sp;

    public MyParams(Context context,SharePreferenceUtils sp) {
        this.context= context;
        this.sp= sp;
    }

//    public Map<String,String> getDefaultParams() {
//        HashMap<String, String> hashMap = new HashMap<>();
//        hashMap.put("app_key",Consts.APP_KEY);
//        hashMap.put("v","1.0");
//        hashMap.put("sk",sp.getSK());
//        return hashMap;
//    }
//    public Map<String,String> getLoginParams() {
//        Map<String, String> hashMap = getDefaultParams();
//        hashMap.put("device", Build.MODEL);
//        hashMap.put("kernel", Build.VERSION.RELEASE);
//        hashMap.put("rom", Build.DISPLAY);
//        hashMap.put("token",getDeviceId(context));
//        return hashMap;
//    }
//
//    public EncodeForm genertorParams(Map<String, String> datas) {
//        logger.debug(formatLog("request params"));
//       String[] arrays =  new String[datas.size()];
//        datas.keySet().toArray(arrays);
//        Arrays.sort(arrays);
//        StringBuilder stringBuilder = new StringBuilder();
//        EncodeForm encodeForm = new EncodeForm();
//        for (int i = 0; i < arrays.length; i++) {
//            String key = arrays[i];
//            String value = datas.get(key);
//            stringBuilder.append(key);
//            stringBuilder.append(value);
//            encodeForm.add(key,value);
//            logger.debug(key+"="+value);
//        }
//
//        String encrypt = EncodeUtils.encrypt(stringBuilder.toString());
//        encodeForm.add("sig", encrypt);
//        logger.debug("sig=" + encrypt);
//        return encodeForm;
//    }

    public static String getDeviceId(Context context) {

        StringBuilder deviceId = new StringBuilder();
        // 渠道标志
        deviceId.append("a");

        // wifi mac地址

        WifiManager wifi = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);

        WifiInfo info = wifi.getConnectionInfo();

        String wifiMac = info.getMacAddress();

        if (wifiMac != null) {

            deviceId.append("wifi");

            deviceId.append(wifiMac);
            return deviceId.toString();

        }
        return  "no_wifi";

    }

    //new
    public RequestParams getDefaultRquestParams() {
        RequestParams params = new RequestParams();
        params.put("app_key",Consts.APP_KEY);
        params.put("v", "1.0");
        params.put("sk", sp.getSK());
        return params;
    }
    public RequestParams getLoginRequestParams() {
        RequestParams params = getDefaultRquestParams();
        params.put("device", Build.MODEL);
        params.put("kernel", Build.VERSION.RELEASE);
        params.put("rom", Build.DISPLAY);
        params.put("token",getDeviceId(context));
        return params;
    }

    public RequestParams genertorReuqestParams(RequestParams params) {
        logger.debug(formatLog("request params"));

        ConcurrentHashMap<String, String> datas = params.getUrlParams();

        String[] arrays =  new String[datas.size()];
        datas.keySet().toArray(arrays);
        Arrays.sort(arrays);
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < arrays.length; i++) {
            String key = arrays[i];
            String value = datas.get(key);
            stringBuilder.append(key);
            stringBuilder.append(value);
            logger.debug(key+"="+value);
        }

        String encrypt = EncodeUtils.encrypt(stringBuilder.toString());
        params.add("sig", encrypt);
        logger.debug("sig=" + encrypt);
        return params;
    }
}
